package cn.ilovese.collect.serviceimpl.task;

import cn.ilovese.collect.datamapper.file.FileMapper;
import cn.ilovese.collect.datamapper.task.TaskMapper;
import cn.ilovese.collect.datamapper.user.UserMapper;
import cn.ilovese.collect.dto.recommend.WorkerGroupRecommend;
import cn.ilovese.collect.po.task.Task;
import cn.ilovese.collect.service.task.TaskService;
import cn.ilovese.collect.util.Constant;
import cn.ilovese.collect.vo.ResultVO;
import cn.ilovese.collect.vo.task.TaskFormVO;
import cn.ilovese.collect.vo.task.TaskVO;
import cn.ilovese.collect.vo.task.TaskWithUsernameVO;
import cn.ilovese.collect.vo.user.UserVO;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
@CacheConfig(cacheNames = "task",cacheManager = "cacheManager")
public class TaskServiceImpl implements TaskService {
    // @Resource标识特定的目标组件，你们自己添加，类似软工二
    // @Autowired装载java bean，用于set方法，你们自己添加，类似软工二
    //注意在这里添加每个实现方法的说明注释，类似以下
    /**
     * 这个方法干了什么
     *
     * @param 参数1 解释
     * @param 参数2 解释
     * ......
     * @return 返回值解释
     */

    @Resource
    TaskMapper taskMapper;
    @Resource
    UserMapper userMapper;
    @Resource
    FileMapper fileMapper;

    @Override
    public ResultVO<List<TaskWithUsernameVO>> getAllTasks() {
        List<Task> temp = taskMapper.selectAll();
        if(temp.size() == 0)
            return new ResultVO<>(Constant.REQUEST_FAIL,"获取任务列表失败");
        List<TaskWithUsernameVO> res = new ArrayList<>();
        for(Task task: temp){
            int uid = task.getUserId();
            String username = userMapper.selectByPrimaryKey(uid).getUsername();
            res.add(new TaskWithUsernameVO(task,username));
        }
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"获取任务列表成功",res);
    }

    @Override
    public ResultVO<List<TaskWithUsernameVO>> getAllValidTasks(){
        Date curr = new Date();
        List<Task> temp = taskMapper.selectValidTasks(new Timestamp(curr.getTime()));
        if(temp.size() == 0)
            return new ResultVO<>(Constant.REQUEST_FAIL,"获取任务列表失败");
        List<TaskWithUsernameVO> res = new ArrayList<>();
        for(Task task: temp){
            int uid = task.getUserId();
            String username = userMapper.selectByPrimaryKey(uid).getUsername();
            res.add(new TaskWithUsernameVO(task,username));
        }
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"获取任务列表成功",res);
    }

    @Override
    public List<TaskVO> getTasksNeedWorker() {
        List<Task> temp = taskMapper.selectTaskNeedsWorker();
        if(temp == null)
            return null;
        List<TaskVO> res = new ArrayList<>();
        for(Task task: temp){
            res.add(new TaskVO(task));
        }
        return res;
    }

    @Override
    public ResultVO<List<TaskWithUsernameVO>> getProducedTasks(Integer uid) {
        List<Task> temp = taskMapper.selectProducedTask(uid, new Timestamp(new Date().getTime()));
        if(temp.size() == 0)
            return new ResultVO<>(Constant.REQUEST_FAIL, "获取任务列表失败");
        List<TaskWithUsernameVO> res = new ArrayList<>();
        for(Task task: temp){
            String bossname = userMapper.selectByPrimaryKey(task.getUserId()).getUsername();
            res.add(new TaskWithUsernameVO(task,bossname));
        }
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"获取任务列表成功",res);
    }

    @Override
    public ResultVO<List<TaskWithUsernameVO>> getCompletedTasks(Integer uid) {
        String bossname =  userMapper.selectByPrimaryKey(uid).getUsername();
        List<Task> temp = taskMapper.selectCompletedTask(uid, new Timestamp(new Date().getTime()));
        if(temp.size() == 0)
            return new ResultVO<>(Constant.REQUEST_FAIL, "获取任务列表失败");
        List<TaskWithUsernameVO> res = new ArrayList<>();
        for(Task task: temp){
            res.add(new TaskWithUsernameVO(task,bossname));
        }
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"获取任务列表成功",res);
    }

    @Override
    public ResultVO<List<TaskWithUsernameVO>> getWorkingTasks(Integer uid) {
        List<Task> temp = taskMapper.selectWorkingTask(uid);
        if(temp.size() == 0)
            return new ResultVO<>(Constant.REQUEST_FAIL, "获取任务列表失败");
        List<TaskWithUsernameVO> res = new ArrayList<>();
        for(Task task: temp){
            String bossname = userMapper.selectByPrimaryKey(task.getUserId()).getUsername();
            res.add(new TaskWithUsernameVO(task,bossname));
        }
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"获取任务列表成功",res);
    }

    @Override
    public ResultVO<List<TaskWithUsernameVO>> getFinishedTasks(Integer uid) {
        List<Task> temp = taskMapper.selectFinishedTask(uid);
        if(temp.size() == 0)
            return new ResultVO<>(Constant.REQUEST_FAIL, "获取任务列表失败");
        List<TaskWithUsernameVO> res = new ArrayList<>();
        for(Task task: temp){
            String bossname = userMapper.selectByPrimaryKey(task.getUserId()).getUsername();
            res.add(new TaskWithUsernameVO(task,bossname));
        }
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"获取任务列表成功",res);
    }

    @Override
    public ResultVO<TaskWithUsernameVO> getTaskById(Integer taskid) {
        Task task = taskMapper.selectByPrimaryKey(taskid);
        if(task == null)
            return new ResultVO<>(Constant.REQUEST_FAIL,"获取详细信息失败");
        String username = userMapper.selectByPrimaryKey(task.getUserId()).getUsername();
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"获取详细信息成功", new TaskWithUsernameVO(task,username));
    }

    @Override
    public ResultVO<TaskVO> createTask(TaskFormVO task) {
        int uid = userMapper.selectByUsername(task.getUsername()).getId();
        TaskVO taskVO = new TaskVO(task,uid);
        Task record = new Task(taskVO);
        int id = taskMapper.insert(record);
        if(id < 1)
            return new ResultVO<>(Constant.REQUEST_FAIL, "插入失败");
        else{
            TaskVO res = new TaskVO(record);
            return new ResultVO<>(Constant.REQUEST_SUCCESS,"插入成功",res);
        }
    }

    @Override
    public ResultVO<TaskVO> updateTask(TaskVO task) {
        int id = task.getId();
        System.out.println(id);
        if(taskMapper.selectByPrimaryKey(id) == null)
            return new ResultVO<>(Constant.REQUEST_FAIL, "不存在此task");
        Task toBeUpdated = new Task(task);
        toBeUpdated.setId(id);
        int res = taskMapper.updateByPrimaryKey(toBeUpdated);
        if(res == 0)
            return new ResultVO<>(Constant.REQUEST_FAIL, "更新失败");
        return new ResultVO<>(Constant.REQUEST_SUCCESS,"更新成功",new TaskVO(toBeUpdated));

    }
    @Override
    public ResultVO<TaskVO> deleteTask(Integer taskid) {
        int id = taskMapper.deleteByPrimaryKey(taskid);
        System.out.println("id:" + id);
        if(id < 1)
            return new ResultVO<>(Constant.REQUEST_FAIL,"删除失败");
        return new ResultVO<>(Constant.REQUEST_SUCCESS, "删除成功");
    }


}
